<template>
  <div>
    <h1>分类列表</h1>
    <el-table :data="items">
      <el-table-column prop="_id" label="ID" width="230"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
      <template slot-scope="scope">
        <!-- ${scope.row_id}表示当前这一行的id -->
        <el-button type="text" size="small" @click="$router.push(`/admin_users/edit/${scope.row._id}`)">编辑</el-button>
        <el-button type="text" size="small" @click="remove(scope.row)">删除</el-button>
      </template>
    </el-table-column>
    </el-table>
    
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: []
    };
  },
  created() {
    this.fetch();
  },
  methods: {
    //加载页面数据
    async fetch() {
      const res = await this.$http.get("rest/admin_user");
      this.items = res.data;
    },
    //删除操作
    async remove(row) {
      //element的MessageBox 弹框组件
      this.$confirm(`是否确定删除管理员 "${row.username}"`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //确定要删除，则调用删除的接口
          await this.$http.delete(`rest/admin_user/${row._id}`)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          //重新渲染页面
          this.fetch()
        })
    }
  }
};
</script>
